package com.baijiayun.live.ui.toolbox.lottery

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import androidx.core.content.ContextCompat
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import com.baijiayun.live.ui.R
import com.baijiayun.live.ui.base.BaseDialogFragment
import com.baijiayun.live.ui.base.BaseFragment
import com.baijiayun.live.ui.base.BasePadFragment
import com.baijiayun.livecore.utils.DisplayUtils
import com.baijiayun.livecore.utils.LPRxUtils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.bjy_layout_lottery_view.*
import java.util.concurrent.TimeUnit

/**
 * Created by pengwenjian on 2020/8/25
 * Describe: 抽奖动画
 */
class LotteryAnimFragment : BasePadFragment() {
    private var timerDispose: Disposable? = null
    private var fallAnim: ObjectAnimator? = null
    private var scaleAnimSet: AnimatorSet? = null
    private var boomAnimSet: AnimatorSet? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        startAnim()
    }
    private fun startAnim() {
        ivEgg.visibility = View.VISIBLE
        ivEgg.setImageResource(R.drawable.bjy_ic_egg_fall)
        fallAnim = ObjectAnimator.ofFloat(ivEgg, "translationY", - DisplayUtils.getScreenHeightPixels(context).toFloat(), 0f)
        fallAnim!!.run {
            interpolator = DecelerateInterpolator()
            duration = 400
            start()
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator?) {
                    // do nothing
                }

                override fun onAnimationEnd(animation: Animator?) {
                    ivEgg.setImageResource(R.drawable.bjy_ic_egg_countdown)
                    tvCountDown.visibility = View.VISIBLE
                    tvCountDown.text = 3.toString()
                    LPRxUtils.dispose(timerDispose)
                    val scaleX = ObjectAnimator.ofFloat(ivEgg, "scaleX", 1f, 0.8f)
                    val scaleY = ObjectAnimator.ofFloat(ivEgg, "scaleY", 1f, 0.8f)
                    scaleAnimSet = AnimatorSet()
                    scaleAnimSet!!.run {
                        playTogether(scaleX, scaleY)
                        duration = 2000
                        interpolator = LinearInterpolator()
                        start()
                    }
                    startTimer()
                }

                override fun onAnimationRepeat(animation: Animator?) {
                    // do nothing
                }

                override fun onAnimationCancel(animation: Animator?) {
                    // do nothing
                }
            })
        }
    }

    private fun startTimer() {
        timerDispose = Observable.interval(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    tvCountDown.text = (2 - it).toString()
                    if (it == 2L) {
                        LPRxUtils.dispose(timerDispose)
                        val boomScaleX = ObjectAnimator.ofFloat(ivEggBoom, "scaleX", 0.9f, 1f)
                        val boomScaleY = ObjectAnimator.ofFloat(ivEggBoom, "scaleY", 0.9f, 1f)
                        val alphaAnim = ObjectAnimator.ofFloat(ivEggBoom, "alpha", 1f, 0.3f)
                        boomAnimSet = AnimatorSet()
                        boomAnimSet!!.run {
                            playTogether(boomScaleX, boomScaleY,alphaAnim)
                            duration = 300
                            interpolator = LinearInterpolator()
                            start()
                            ivEggBoom.visibility = View.VISIBLE
                            tvCountDown.visibility = View.INVISIBLE
                            ivEgg.visibility = View.INVISIBLE
                            scaleAnimSet?.cancel()
                            addListener(object : Animator.AnimatorListener {
                                override fun onAnimationRepeat(animation: Animator?) {
                                    // do nothing
                                }

                                override fun onAnimationEnd(animation: Animator?) {
                                    ivEggBoom.visibility = View.GONE
                                    listener?.onAnimDismiss()
                                }

                                override fun onAnimationCancel(animation: Animator?) {
                                    ivEggBoom.visibility = View.GONE
                                    listener?.onAnimDismiss()
                                }

                                override fun onAnimationStart(animation: Animator?) {
                                    // do nothing
                                }
                            })
                        }
                        return@subscribe
                    }
                }
    }

    fun destroy() {
        LPRxUtils.dispose(timerDispose)
        fallAnim?.cancel()
        fallAnim?.removeAllListeners()
        scaleAnimSet?.cancel()
        boomAnimSet?.cancel()
        boomAnimSet?.removeAllListeners()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        destroy()
    }


    override fun getLayoutId() = R.layout.bjy_layout_lottery_view

    private var listener: AnimListener? = null

    fun setAnimListener(listener: AnimListener) {
        this.listener = listener
    }
    interface AnimListener{
        fun onAnimDismiss()
    }
}